package com.example.demo.quartz.service.serviceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.base.PageRequestBean;
import com.example.demo.entity.base.PageResponseBean;
import com.example.demo.entity.base.ResponseEntity;
import com.example.demo.entity.mybatisplus.TbSysCustomQuartz;
import com.example.demo.quartz.dao.MpTbSysCustomQuartzDao;
import com.example.demo.quartz.service.MpTbSysCustomQuartzService;
import com.example.demo.util.ResponseFactory;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 自定义定时任务表 服务实现类
 * </p>
 *
 * @author xzw
 * @since 2021-04-28
 */
@Slf4j
@Service
@Transactional
public class MpTbSysCustomQuartzServiceImpl extends ServiceImpl<MpTbSysCustomQuartzDao, TbSysCustomQuartz> implements MpTbSysCustomQuartzService {

	@Autowired
	private MpTbSysCustomQuartzDao mpTbSysCustomQuartzDao;
	
	@Override
	public ResponseEntity<PageResponseBean<Map<String, Object>>> queryCustomQuartz(PageRequestBean<TbSysCustomQuartz> pagerequest) {
		
		int pageNum = pagerequest.getPageNum();
		
		int pageSize = pagerequest.getPageSize();
		
		TbSysCustomQuartz params = pagerequest.getParams();
		
		Page page = PageHelper.startPage(pageNum, pageSize, true);
		
		List<Map<String, Object>> customQuartzs = null;
		
		try {
			customQuartzs = mpTbSysCustomQuartzDao.queryCustomQuartz(params);
		} catch (Exception e) {
			log.error("分页查询自定义定时器失败",e);
			return ResponseFactory.setFail("分页查询自定义定时器失败");
		}
		
		PageResponseBean<Map<String, Object>> pageresponse = new PageResponseBean<>(page, customQuartzs);
		
		return ResponseFactory.setObject(pageresponse);
	}

	@Override
	public ResponseEntity<String> addCustomQuartz(TbSysCustomQuartz customQuartz) {
		
		customQuartz.setCreate_time(new Date());
		customQuartz.setUpdate_time(new Date());
		
		try {
			mpTbSysCustomQuartzDao.insert(customQuartz);
		} catch (Exception e) {
			log.error("新增自定义定时任务失败",e);
			return ResponseFactory.setFail("新增自定义定时任务失败");
		}
		
		
		return ResponseFactory.setSuccess("新增定时任务成功!");
	}

	@Override
	public ResponseEntity<String> updateCustomQuartz(TbSysCustomQuartz customQuartz) {
		customQuartz.setUpdate_time(new Date());
		
		QueryWrapper<TbSysCustomQuartz> wrapper = new QueryWrapper<>();
		Map<String, Object> param = new HashMap<>();
		
		String job_name = customQuartz.getJob_name();
		String job_group = customQuartz.getJob_group();
		param.put("job_name", job_name);
		param.put("job_group", job_group);
		wrapper.allEq(param);
		
		
		try {
			mpTbSysCustomQuartzDao.update(customQuartz, wrapper);
		} catch (Exception e) {
			log.error("修改自定义定时任务失败",e);
			return ResponseFactory.setFail("新增自定义定时任务失败");
		}
		
		return ResponseFactory.setSuccess("修改自定义定时任务成功");
	}

	@Override
	public ResponseEntity<String> deleteCustomQuartz(TbSysCustomQuartz customQuartz) {
		QueryWrapper<TbSysCustomQuartz> wrapper = new QueryWrapper<>();
		Map<String, Object> param = new HashMap<>();
		
		String job_name = customQuartz.getJob_name();
		String job_group = customQuartz.getJob_group();
		param.put("job_name", job_name);
		param.put("job_group", job_group);
		
		mpTbSysCustomQuartzDao.deleteByMap(param);
		
		return ResponseFactory.setSuccess("删除自定义定时任务成功");
	}

}
